var fs = require('fs');
var path = require('path');

module.exports = {
    mkdirs: function (dirpath, callback, mode) {
        var that = this;
        mode = mode || 0777;
        fs.exists(dirpath, function (exists) {
            if (exists) {
                callback(null);
            } else {
                //尝试创建父目录，然后再创建当前目录
                that.mkdirs(path.dirname(dirpath), function () {
                    fs.mkdir(dirpath, mode, callback);
                }, mode);
            }
        });
    },

    deldirs: function (path) {
        var that = this;
        if (fs.existsSync(path)) {
            fs.readdirSync(path).forEach(function (file) {
                var curPath = path + "/" + file;
                if (fs.statSync(curPath).isDirectory()) {
                    that.deldirs(curPath);
                } else {
                    fs.unlinkSync(curPath);
                }
            });
            fs.rmdirSync(path);
        }
    },

    getFileExt: function (filePath) {
        var ext = path.extname(filePath);
        return ext ? ext.slice(1) : '';
    },

    getFileInfo: function (filePath) {
        return path.parse(filePath);
    }
};